import datetime
from PIL import Image, ImageDraw, ImageFont


def save2(text_input):
    newline_count = text_input.count("\n")
    print("字符串中换行符的次数：", newline_count)
    linepix = int(1024 * (newline_count + 4) / 32)
    # 创建一个空白图像
    width, height = 1080, linepix
    background_color = (47, 79, 79)
    image = Image.new("RGB", (width, height), background_color)
    draw = ImageDraw.Draw(image)

    # 在图像上绘制文本
    text = text_input
    font_size = 30
    font = ImageFont.truetype("Arial.ttf", font_size)  # 使用合适的字体文件路径
    text_color = (192, 192, 192)
    text_position = (50, 50)
    draw.text(text_position, text, fill=text_color, font=font)

    # 保存图像
    image.save("/www/wwwroot/pythonproject/nuker2/test.png")
    print("save png")


if __name__ == '__main__':
    try:
        current_time = datetime.datetime.now()
        print(current_time)
        save2(str(current_time))
    except:
        print("crush")
